package com.example.controller;

import com.example.dto.EasyUIDataGridResult;
import com.example.dto.EasyUITreeNode;
import com.example.dto.Result;
import com.example.entity.TbItem;
import com.example.service.ItemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2018/7/15.
 */
@Controller
@RequestMapping("/item")
public class ItemController {
    @Resource
    private ItemService itemService;

    @RequestMapping("/list")
    @ResponseBody
    public EasyUIDataGridResult list(@RequestParam Integer page, @RequestParam Integer rows) {
        return itemService.getItemList(page, rows);
    }

    @RequestMapping("/cat/list")
    @ResponseBody
    public List<EasyUITreeNode> getItemCatList(@RequestParam(value = "id", defaultValue = "0") Long parentId) {
        return itemService.getItemCatList(parentId);
    }

    @RequestMapping("/{itemID}")
    @ResponseBody
    public List<EasyUITreeNode> getItemByID(@PathVariable String itemID) {
        return null;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result saveItem(TbItem tbItem, String desc, String itemParams) {
        return itemService.saveItem(tbItem, desc, itemParams);
    }

    @RequestMapping("/itemParam/{itemId}")
    public String showItemParam(@PathVariable Long itemId, Model model) {
        String itemParamHtml = itemService.getItemParamHtml(itemId);
        model.addAttribute("itemParamHtml", itemParamHtml);
        return "itemParam";
    }


}
